# coding: utf-8

import os
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageEnhance
from handler.image import *
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

image_url = 'http://service.cpic.com.cn/common/generateCaptcha?1516336380426'
image = download_to_image(image_url)
# draw = ImageDraw.Draw(image)
enhancer = ImageEnhance.Contrast(image)
image = enhancer.enhance(2)
image = image.convert('1')
w, h = image.size
data = image.getdata()
###################################################
color_red = (255, 0, 0)
data_value = dict()
for y in range( 1, h-1):
    data_value[y] = dict()
    for x in range(1, w-1):
        # rgb = data[w * y + x]
        rgb = image.getpixel((x, y))
        data_value[y][x] = rgb == 0 and 1 or 0
        # print('({0},{1})\t=\t{2}'.format(x, y, rgb))
        if rgb == 0:
            range_size = 2
            black_number = 0
            i = 1
            if image.getpixel((x-i, y)) == 0: black_number += 1
            if image.getpixel((x+i, y)) == 0: black_number += 1
            if image.getpixel((x, y-i)) == 0: black_number += 1
            if image.getpixel((x, y+i)) == 0: black_number += 1
            if image.getpixel((x-i, y-i)) == 0: black_number += 1
            if image.getpixel((x+i, y-i)) == 0: black_number += 1
            if image.getpixel((x-i, y+i)) == 0: black_number += 1
            if image.getpixel((x+i, y+i)) == 0: black_number += 1
            # print('black_number={0}'.format(black_number))
            if black_number >= 9:
                image.putpixel((x, y), 255)
            else:
                image.putpixel((x, y), 0)
                # draw.point((x, y), color_red)
image.show()
###################################################
# np_array = np.asarray(image)
# def gray_inver(img):
#     for x in range(img.size[0]):
#         for y in range(img.size[1]):
#             r, g, b = img.getpixel((x, y))
#             img.putpixel((x, y), (255 - r, 255 - g, 255 - b))
# gray_inver(image)
# image.show()
# img = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
# kernel = np.uint8(np.zeros((3, 3)))
# for x in range(3):
#     kernel[x, 2] = 1
#     kernel[2, x] = 1
# eroded = cv2.erode(img, kernel)
# new_image = Image.fromarray(cv2.cvtColor(eroded, cv2.COLOR_BGR2RGB))
# # gray_inver(image)
# new_image.show()
# # image = Image.fromarray(cv2.cvtColor(np_array, cv2.COLOR_BGR2RGB))
# code = image_to_string(image)
# print('code={0}'.format(code))
# exit()
###################################################
# def get_left_start_point(im):
#     start_point = (0, 0)
#     found = False
#     w, h = im.size
#     data = list(im.getdata())
#     for x in xrange(w):
#         for y in xrange(h):
#             print('{0}={1}'.format(y * w + x, data[y * w + x]))
#             if data[y * w + x] != 255:
#                 found = True
#                 start_point = (x, y)
#                 break
#
#         if found:
#             break
#     return start_point
#
# start_point = get_left_start_point(image)

# new_image = Image.new('RGB', image.size, (255, 255, 255))
for y,y_value in data_value.items():
    string = ''
    for x,x_value in y_value.items():
        # if x_value == 1:
        #     new_image.putpixel((x, y), (255, 0, 0))
        string += '{0}'.format(x_value)
        # string += '{0}'.format(x_value == 0 and ' ' or x_value)
        # print('({0},{1})={2}'.format(x, y, y_value))
    print(string)

# new_image.putpixel(start_point, 0)
# new_image.putpixel((start_point[0], start_point[1] + 1), 0)
# new_image.putpixel((start_point[0], start_point[1] - 1), 0)
# new_image.putpixel((start_point[0] + 1, start_point[1]), 0)
# new_image.putpixel((start_point[0] - 1, start_point[1]), 0)
# new_image.putpixel((start_point[0] + 1, start_point[1] + 1), 0)
# new_image.putpixel((start_point[0] - 1, start_point[1] + 1), 0)
# new_image.putpixel((start_point[0] + 1, start_point[1] - 1), 0)
# new_image.putpixel((start_point[0] - 1, start_point[1] - 1), 0)
# new_image.show()